package com.stackoverflow;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;

public class SiteMenu extends Activity implements OnClickListener {

	private static final int SEARCH_DIALOG_ID = 0;
	private static final int USER_SEARCH_DIALOG_ID = 1;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.sitemenu);
	    
	    View recentButton = findViewById(R.id.recent);
        recentButton.setOnClickListener(this);
        
        View topTagsButton = findViewById(R.id.toptags);
        topTagsButton.setOnClickListener(this);
        
        View searchButton = findViewById(R.id.topicsearch);
        searchButton.setOnClickListener(this);
        
        View userSearchButton = findViewById(R.id.usersearch);
        userSearchButton.setOnClickListener(this);
        
        View profileButton = findViewById(R.id.myprofile);
        profileButton.setOnClickListener(this);
	}
	
	@Override
	public void onResume()
	{
		super.onPause();
		Retriever.getInstance().clearQuery();
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	final View layout = inflater.inflate(R.layout.search_dialog, (ViewGroup) findViewById(R.id.root));  	    	

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
                	SiteMenu.this.dismissDialog(SEARCH_DIALOG_ID);
                }
            });
		
		switch (id) {
    		case SEARCH_DIALOG_ID:
    			builder.setTitle("Search for Topic").setView(layout);
                builder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int whichButton) {
                        	SiteMenu.this.dismissDialog(SEARCH_DIALOG_ID);
                        	EditText search_field = (EditText) layout.findViewById(R.id.search_field);
                        	
                        	Intent i = new Intent(SiteMenu.this, QuestionList.class);
                        	
                        	String tagString = "{search}"+search_field.getText().toString();

                        	i.putExtra("tagName", tagString);
                        	startActivity(i);
						}                	
                    });
                return builder.create();
    		case USER_SEARCH_DIALOG_ID:
    			builder.setTitle("Search for User").setView(layout);
    			builder.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
    						
    						public void onClick(DialogInterface dialog, int whichButton) {
                            	SiteMenu.this.dismissDialog(USER_SEARCH_DIALOG_ID);
                            	EditText search_field = (EditText) layout.findViewById(R.id.search_field);
                            	
                            	Intent i = new Intent(SiteMenu.this, UserList.class);
                            	
                            	String userName = search_field.getText().toString();

                            	i.putExtra("userName", userName);
                            	startActivity(i);
    						}                	
                        });
    			return builder.create();
    	    default:
    	    	return null;
    	}
    }
    
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        
        AlertDialog a = (AlertDialog) dialog;
    	EditText name_field = (EditText) a.findViewById(R.id.search_field);
    	name_field.setText("");
    }
	
	public void onClick(View v) {
    	switch (v.getId()) {
    	case R.id.recent:
    		startActivity(new Intent(this, QuestionList.class));
    		break;
    	case R.id.toptags:
    		startActivity(new Intent(this, TopTags.class));
    		break;
    	case R.id.topicsearch:
    		showDialog(SEARCH_DIALOG_ID);
    		break;
    	case R.id.usersearch:
    		showDialog(USER_SEARCH_DIALOG_ID);
    		break;
    	case R.id.myprofile:
    		startActivity(new Intent(this, ProfileDisplay.class));
    		break;
    	}
    }
}
